using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using MyFrame.Pool;
using Platform.CharacterData;
using UnityEngine;
using Plugin.Tool;

namespace Platform.Skill.Deployer
{
    public class CrystalSkillDeployer : SkillDeployer
    {
        //记录是否传承
        public bool transferTag = false;

        public bool canExplode = true;

        private bool canGrow;
        [SerializeField] private Vector2 growSize = new Vector2(3f, 3f);

        // 移动式水晶，和传送式互斥
        private Transform closestTarget;
        private bool canMove;
        public float moveSpeed = 5f;

        private bool canClone = true;


        void Awake()
        {
            anim = GetComponent<Animator>();
        }

        void Update()
        {
            if (canMove && closestTarget != null)
            {
                transform.position = Vector2.MoveTowards(transform.position, closestTarget.GetComponent<Enemy>().GetRealPos(), moveSpeed * Time.deltaTime);
                if (Vector2.Distance(transform.position, closestTarget.position) < 1f)
                {
                    ZTimer.ClearTimer(EndEffect);
                    EndEffect();
                    canMove = false;
                }
            }
        }

        public ulong timerId;
        public override void DeploySkill()
        {
            if (skillData.skillId == (int)SkillEnum.ExplodeCrstal)
                canMove = true;
            else canMove = false;
            // 移动式  因为是预先填充的，所以要重置位置
            if (canMove)
                skillData.skillPrefab.transform.position = skillData.owner.transform.position;

            chooseTargets();
            closestTarget = Tool.FindClosestEnemy(transform);

            ZTimer.ClearTimer(EndEffect);
            // ZTimer.ClearTimer(timerId);
            // StopCoroutine(Overtimer());
            if (transferTag)
            {
                if (canMove) return;

                // 生成在交换前位置
                if (canClone) OwnerEntity.UseSkill(SkillEnum.Clone, OwnerPos);
                var playerPos = skillData.owner.transform.position;
                base.DeploySkill();
                transform.position = playerPos;

                EndEffect();
                return;
            }
            transferTag = true;

            //超时销毁
            // StartCoroutine(Overtimer());
            // timerId = ZTimer.SetTimeout(skillData.durationTime, EndEffect);
            ZTimer.SetTimeout(skillData.durationTime, EndEffect);
        }
        private IEnumerator Overtimer()
        {
            yield return new WaitForSeconds(skillData.durationTime);
            EndEffect();
        }


        public override void EndEffect()
        {
            //已经爆炸，，不能再调用该对象，会重复删除
            skillData.skillPrefab = null;
            // ZTimer.ClearTimer(EndEffect);
            if (canExplode && anim != null)
            {
                anim.SetTrigger("Explode");
                canGrow = true;
            }

            base.EndEffect();
            transferTag = false;

            if (canExplode)
                StartCoroutine(DestroyAfterAnimation());
            else
                Destroy(gameObject);
        }

        IEnumerator DestroyAfterAnimation()
        {

            AnimatorClipInfo[] clipInfo = anim.GetCurrentAnimatorClipInfo(0);
            float clipLength = clipInfo[0].clip.length;

            if (canGrow) transform.DOScale(growSize, clipLength).SetEase(Ease.InOutQuad);
            // 等待动画完成
            yield return new WaitForSeconds(clipLength);
            Destroy(gameObject);
        }



        private void AnimationExplodeEvent()
        {
            chooseTargets();
            AttackStart();
        }
    }
}